package com.efast.dmom.mgmt.service.mes.report.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.efast.cafe.commponent.query.service.impl.QueryServiceImpl;
import com.efast.cafe.framework.bean.QueryPageBase;
import com.efast.cafe.framework.jdbc.JdbcDao;
import com.efast.cafe.portal.bean.query.QueryBean;
import com.efast.cafe.portal.bean.query.QueryConditionBean;
import com.efast.cafe.portal.bean.query.QueryOpBean;
import com.efast.dmom.mgmt.dao.mes.report.ReportVehicleRetrospectDao;
import com.efast.dmom.mgmt.entity.report.ReportVehicleRetrospect;

@Service("apiindex533-1001")
public class ReportQualityInfoServiceImpl extends QueryServiceImpl{
	@Autowired
	private ReportVehicleRetrospectDao reportVehicleRetrospectDao;
	@Autowired
	@Qualifier("jdbcDao")
	private JdbcDao jdbcDao;
	@Override
	public Object queryList(QueryBean querys) {
		QueryPageBase result = new QueryPageBase();
		QueryConditionBean queryConditionBean = querys.getQueryConditionBean();
		String sql = "";
		String sql2 = "SELECT\r\n" + 
				"	plan.plan_order_no AS planOrderNO,\r\n" + 
				"	plant.plant_name_c AS plantNameC,\r\n" + 
				"	workshop.workshop_name_c AS workshopNameC,\r\n" + 
				"	rule.model AS model,\r\n" + 
				"	lot.vin as vin,\r\n" + 
				"	lot.self_number as selfNumber,\r\n" + 
				"	defect.station AS station,\r\n" + 
				"	defect.area AS area,\r\n" + 
				"	defect.part AS part,\r\n" + 
				"	p.part_name_c AS partName,\r\n" + 
				"	defect.item AS item,\r\n" + 
				"	defect.defect_code AS defectCode,\r\n" + 
				"	date_format( defect.create_time, '%Y-%m-%d %H:%i:%s' ) AS createTime,\r\n" + 
				"	defect.create_user AS createUser,\r\n" + 
				"	defect.status AS status  \r\n" + 
				"  FROM qm_bas_defect_record defect	\r\n" + 
				"	left JOIN tw_bas_lot lot  ON lot.lot_name = defect.lot_name	\r\n" + 
				"	LEFT JOIN tw_bas_work_order o ON lot.order_id = o.order_id\r\n" + 
				"	LEFT JOIN inf_pp_plan plan ON o.plan_id = plan.id\r\n" + 
				"	LEFT JOIN tr_bas_product_config config ON config.operation_id = lot.operation_id\r\n" + 
				"	LEFT JOIN tm_bas_station station ON station.station_name = defect.station\r\n" + 
				"	LEFT JOIN tm_bas_production_line pl ON pl.line_name = station.line_name\r\n" + 
				"	LEFT JOIN tm_bas_area area ON area.area_no = pl.area_no\r\n" + 
				"	LEFT JOIN tm_bas_workshop workshop ON workshop.workshop_no = area.workshop_no\r\n" + 
				"	LEFT JOIN tm_bas_plant plant ON workshop.tm_bas_plant_id = plant.tm_bas_plant_id	\r\n" + 
				"	LEFT JOIN tm_bas_part AS p ON p.part_no = defect.part\r\n" + 
				"	LEFT JOIN pm_vinrule_config rule on rule.part_no = lot.part_no_version where lot.lot_name not like '%-A' ";
		String selfLotName =""; 
		String vinLotName = "";
		String selfNumber = "";
		Integer start = 0;
		Integer length = 0;
		if(queryConditionBean.getStart() == null || queryConditionBean.getLength() == null) {
			start = 0;
			length =querys.getTemplate().getPagenation();
		}
	    start = queryConditionBean.getStart();
		length = queryConditionBean.getLength();
		List<QueryOpBean> list = queryConditionBean.getQueryOpBeans();
		for(int i=0;i<list.size();i++) {
			String key = list.get(i).getKey().trim();
			String value = list.get(i).getValue().trim();
			if(key!=null &&!"".equals(key)&& value!=null && !"".equals(value) ) {
				if(key.equals("lot.vin")) {
					//获取车辆合拼信息
					List<ReportVehicleRetrospect> spliceInfo = reportVehicleRetrospectDao.getSpliceInfo(value);
					if(spliceInfo!=null && spliceInfo.size()>0) {
						for (int j=0;j<spliceInfo.size();j++) {
							
							//说明为货车合拼之后的查询
							 vinLotName = spliceInfo.get(j).getLotName();						
							 selfNumber = spliceInfo.get(j).getSelfNumber();
							 List<ReportVehicleRetrospect> selfLotNameList = reportVehicleRetrospectDao.getSelfLotName(selfNumber);
							 if(selfLotNameList!=null &&selfLotNameList.size()>0) {
								 selfLotName= selfLotNameList.get(0).getLotName();
							 }
							 if(j==0) {
								 sql+="  defect.lot_name like '%"+vinLotName+"%' or defect.lot_name like '%"+selfLotName+"%' ";
							 }else {
								 sql+=" or defect.lot_name like '%"+vinLotName+"%' or defect.lot_name like '%"+selfLotName+"%' ";
							 }
							 
						}
						sql="and ("+sql+"or lot.vin like '%"+value+"%' or lot.self_number like '%"+value+"%' )";
					}else {
						sql+="and (lot.vin like '%"+value+"%' or lot.self_number like '%"+value+"%' )";
					}
					
				}else if(key.equals("defect.create_time")) {
					if(value!=null &&value.length()>0) {
						String[] split = value.split("~");
						if(split!=null &&split.length>1) {
							String beginTime = split[0];
							String endTime = split[1];
							sql+="and "+key+" between '"+beginTime+"' and '"+endTime+"'";
						}
						
					}
					
				}else {
					sql+="and "+key+" like '%"+value+"%'";
				}
			}
		}
		sql = sql2+sql;
		sql+=" group by defect.id";
		int count = jdbcDao.queryCount(sql);
		sql+=" order by defect.create_time DESC";
		if(length!=null && length!=0) {
			sql+=" limit "+start+","+length;	
		}		
		List<Map<String, Object>> results = jdbcDao.queryForListMap(sql);
		result.setResultList(results);
		result.setTotalRows(count);
		return result;
		
	}
}
